#!/usr/bin/env python
# _*_ coding:utf-8 _*_


__author__ = 'Young'
'''
校验当前返回影响结果是否为 json类型：  

1.使用data参数，报文是dict类型，如果不指定headers中content-type的类型，默认application/x-www-form-urlencoded，相当于普通form表单提交的形式，会将表单内的数据转换成键值对，此时数据可以从request.POST里面获取，
 而request.body的内容则为a=1&b=2的这种键值对形式。
注意：即使指定content-type=application/json，request.body的值也是类似于a=1&b=2，所以并不能用json.loads(request.body.decode())得到想要的值。

2. 使用data参数，报文是str类型，如果不指定headers中content-type的类型，默认application/json。
 
'''

from log.logUtils import logger


class checkJSON(object):
    def getKeys(self, data):
        keysAll_list = []

        def getkeys(data):  # 遍历json所有key
            if (type(data) == type({})):
                keys = data.keys()
                for key in keys:
                    value = data.get(key)
                    if (type(value) != type({}) and type(value) != type([])):
                        keysAll_list.append(key)
                    elif type(value) == type({}):
                        keysAll_list.append(key)
                        getkeys(value)
                    elif type(value) == type([]):
                        keysAll_list.append(key)
                        for para in value:
                            if (type(para) == type({}) or type(para) == type([])):
                                getkeys(para)
                            else:
                                keysAll_list.append(para)

        getkeys(data)
        return keysAll_list

    def isExtend(self, data, tagkey):  # 检测目标字段tagkey是否在data(json数据)中
        if (type(data) != type({})):
            logger.info('please input a json!')
        else:
            key_list = self.getKeys(data)
            for key in key_list:
                if (key == tagkey):
                    return True
        return False


if __name__ == '__main__':
    '''
    dic = {'responseTime': '2021-03-02 09:33:47', 'code': '200', 'msg': 'SUCCESS', 'data': {'token': {
        'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsYXN0TG9naW5UaW1lIjoxNjE0NTg0MjQyLCJsb2dpblR5cGUiOjEsInBob25lIjoiMTQ5OTk5OTk5OTkiLCJwbGF0Zm9ybVR5cGUiOjEsInBpZCI6MSwiZXhwIjoxNjIyMzYwMjQyLCJ1dWlkIjoxMzY2MjkxNDU2Mzg1ODg0MTYxfQ.-jPTdUZhznuYy6VTGFYO8r13k6ctfCprgL-FXSpf6Bo',
        'iat': None, 'exp': None}, 'bsInfo': {'phone': '149****9999', 'phoneDecrypted': '14999999999',
                                              'uuid': 1366291456385884161, 'gradeName': '', 'grade': '', 'gender': 0,
                                              'nickname': '149****9999',
                                              'avatarImg': 'https://resource.53zaixian.com/common/images/avatar.png',
                                              'qq': '', 'schoolName': '', 'wechat': '', 'motto': ''}, 'bindQq': None,
                                                                                            'bindWeChat': None,
                                                                                            'bindApple': None,
                                                                                            'uuid': 1366291456385884161,
                                                                                            'selectGrade': 1,

                                                                                    'registerNew': False}}
    logger.info(dic)
    logger.info(type(dic))
    isFalse = checkJSON().isExtend(dic, 'token')
    logger.info(checkJSON().isExtend(dic, 'token'))
    pass
    '''
